<?php

namespace Home\Controller;

/**
 * Description of InterfaceServerController.class控制器实例
 *
 * @author wenhui
 * @date 2017-11-14
 */
class InterfaceServerController extends ParentController {

    protected function _initialize() {
        parent::_initialize();
        $this->validateUtil = new \Org\Util\Validate();
    }

    /**
     * @description 申请接口服务
     * @modify wenhui 2017-11-14
     */
    public function applyServer() {
        $param = I('post.');
        $ValidateRule = array(
            'hotel' => 'notempty,notnull,id',
            'url' => 'notempty,notnull',
            'serverId' => 'notempty,notnull,id',
        );

        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($param['hotel']);

        // 从 URL 中取得域名
        $urlInfo = parse_url(trim($param['url']));
        //验证参数
        $this->ajaxValidateReturn($this->validateUtil, $param, $ValidateRule);
        $this->ajaxValidateReturn($this->validateUtil, $urlInfo['host'], 'hostRegex');

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $serverId = $param['serverId'];
        $url = trim($param['url']);

        if (strpos($url, 'http') === false) {
            returnJson(403, 805);
            return;
        }

        $hotelLogic = A('Common/Hotel', 'Logic');
        // 检查客栈是否在有效期内
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        // 检查客栈启用、禁用状态
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }
        // 检查客栈使用状态
        $result = $hotelLogic->checkHotelUsage($hotelId, [1]);
        if ($result == false) {
            returnJson(403, 429);
            return;
        }

        // 检查客栈是否已经开通微预定
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            returnJson(403, 409);
            return;
        }

        // 申请接口服务
        $interfaveServerLogic = A('Common/InterfaceServer', 'Logic');
        $result = $interfaveServerLogic->addInterfaceServer($hotelId, $url, $serverId);
        if ($result == false) {
            returnJson(500, 508);
        }

        returnJson(200, 200);
    }

    /**
     * @description 申请接口服务
     * @modify wenhui 2017-11-14
     */
    public function setApplyInfo() {
        $param = I('post.');
        $ValidateRule = array(
            'hotel' => 'notempty,notnull,id',
            'serverId' => 'notempty,notnull,id',
        );

        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($param['hotel']);

        $url = isset($param['url']) ? trim($param['url']) : null;
        $token = isset($param['token']) && $param['token'] == 1 ? $param['token'] : null;

        // 从 URL 中取得域名
        if (isset($param['url'])) {
            $urlInfo = parse_url(trim($param['url']));
            $this->ajaxValidateReturn($this->validateUtil, $urlInfo['host'], 'hostRegex');
            if (strpos($param['url'], 'http') === false) {
                returnJson(403, 805);
                return;
            }
        }

        //验证参数
        $this->ajaxValidateReturn($this->validateUtil, $param, $ValidateRule);

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $serverId = $param['serverId'];

        $hotelLogic = A('Common/Hotel', 'Logic');
        // 检查客栈是否在有效期内
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        // 检查客栈启用、禁用状态
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }
        // 检查客栈使用状态
        $result = $hotelLogic->checkHotelUsage($hotelId, [1]);
        if ($result == false) {
            returnJson(403, 429);
            return;
        }

        // 检查客栈是否已经开通微预定
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            returnJson(403, 409);
            return;
        }

        // 申请接口服务
        $interfaveServerLogic = A('Common/InterfaceServer', 'Logic');
        $result = $interfaveServerLogic->saveInterfaceServer($hotelId, $serverId, $url, $token);
        if ($result == false) {
            returnJson(500, 509);
        }

        returnJson(200, 200, array('token'=>$result));
    }
}